/*------------------------------------------------------------------------------------------------\
| Horrible X-Wing game by Steven Haber
| st_haber@hotmail.com
|
| xwing.cpp: contains main
\------------------------------------------------------------------------------------------------*/

#include "xwing.h"	//Contains all the tuning definitions and all the other headers


int main()
{
	std::cout << "Loading...\n";

	//Loading sounds
	Sound sounds;	

	//Loading sprites
	sf::Sprite *sprites[SPRITES];

	sf::Image starImage;
	if(!starImage.LoadFromFile("star.bmp"))
		return EXIT_FAILURE;
	sprites[STAR] = new sf::Sprite(starImage);

	sf::Image craftImage;
	if(!craftImage.LoadFromFile("craft.bmp"))
		return EXIT_FAILURE;
	craftImage.CreateMaskFromColor(sf::Color::White);
	sprites[XWING] = new sf::Sprite(craftImage);

	sf::Image craftImageP2;
	if(!craftImageP2.LoadFromFile("craftP2.bmp"))
		return EXIT_FAILURE;
	craftImageP2.CreateMaskFromColor(sf::Color::White);
	sprites[XWING_BLUE] = new sf::Sprite(craftImageP2);

	sf::Image healthImage1;
	if(!healthImage1.LoadFromFile("healthbar_under.bmp"))
		return EXIT_FAILURE;
	healthImage1.CreateMaskFromColor(sf::Color::White);
	sprites[HEALTH_U] = new sf::Sprite(healthImage1);

	sf::Image healthImage2;
	if(!healthImage2.LoadFromFile("healthbar_over.bmp"))
		return EXIT_FAILURE;
	healthImage2.CreateMaskFromColor(sf::Color::White);
	sprites[HEALTH_O] = new sf::Sprite(healthImage2);

	sf::Image layzarImage1;
	if(!layzarImage1.LoadFromFile("layzar_red.bmp"))
		return EXIT_FAILURE;
	sprites[LAYZAR_R] = new sf::Sprite(layzarImage1);

	sf::Image layzarImage2;
	if(!layzarImage2.LoadFromFile("layzar_green.bmp"))
		return EXIT_FAILURE;
	sprites[LAYZAR_G] = new sf::Sprite(layzarImage2);

	sf::Image menuImage;
	if(!menuImage.LoadFromFile("menu.bmp"))
		return EXIT_FAILURE;
	sprites[MENU] = new sf::Sprite(menuImage);

	sf::Image tieImage;
	if(!tieImage.LoadFromFile("tie.bmp"))
		return EXIT_FAILURE;
	tieImage.CreateMaskFromColor(sf::Color::White);
	sprites[TIE] = new sf::Sprite(tieImage);


	//RenderWindow created
	std::cout << "Opening OpenGL rendering window...\n";
	sf::RenderWindow xwing(sf::VideoMode(SCREENX, SCREENY), "X-Wing", sf::Style::VIDEOMODE);
	xwing.ShowMouseCursor(false);
	//xwing.SetFramerateLimit(FRAMERATE);
	xwing.SetBackgroundColor(sf::Color::Black);
	xwing.Display();


	//Stars created
	Star *stars[STARS];
	for(int i = 0; i < STARS; i++)
		stars[i] = new Star(sprites, i, &xwing);


	//Starting the awesome music
	sf::Music awesomeMusic;
	if(!awesomeMusic.Open("music.ogg"))
		return EXIT_FAILURE;
	awesomeMusic.SetVolume(MUSICVOLUME);
	awesomeMusic.SetLoop(true);
	awesomeMusic.Play();
	

	//Calling STARTUP menu
	Menu menu(sprites, &xwing, &sounds);
	int numberOfPlayers = menu.showMenu(STARTUP);	//Number of players determined
	

	//Players initialized
	Player *player1 = new Player(1, sprites, XWINGOFFSETX, XWINGOFFSETY, &xwing, &sounds);
	Player *player2;
	if(numberOfPlayers == 2)
		player2 = new Player(2, sprites, XWINGOFFSETX, XWINGOFFSETY, &xwing, &sounds);

	
	//AI initialized
	Ai intel(*sprites[TIE], sprites[LAYZAR_G], TIEOFFSETX, TIEOFFSETY, &xwing, &sounds, 25);
	intel.setManual(false);


	//Initializing game loop
	sf::Clock timer;	//Used for waiting for things
	float delta;	//Used for change in time
	bool runningFlag = true;
	bool player1Alive = true;
	bool player2Alive = true;
	bool alive;
	bool gameOver = false;
	

	//Main game loop starts
	while(runningFlag)
	{
		delta = xwing.GetFrameTime();
		
		sf::Event event;


		//Exit check
		while(xwing.GetEvent(event))
		{
			if(event.Type == sf::Event::Closed)	//Checks for the X button
				runningFlag = false;
		}


		//Gameover check
		if(numberOfPlayers == 1)
		{
			if(player1Alive == false)
			{
				if(!gameOver)
				{
					awesomeMusic.Stop();
					gameOver = true;
					timer.Reset();
				}

				if(timer.GetElapsedTime() > DEADTIME)
					menu.showMenu(ENDGAME);
			}
		}

		else
		{			
			if(gameOver || (!player1Alive && !player2Alive))
			{
				if(!gameOver)
				{
					awesomeMusic.Stop();
					gameOver = true;
					timer.Reset();
				}

				if(timer.GetElapsedTime() > DEADTIME)
					menu.showMenu(ENDGAME);
			}
		}
		

		//Pause or menu check
		if(xwing.GetInput().IsKeyDown(sf::Key::Escape))
		{
			awesomeMusic.SetVolume(PAUSEVOLUME);
			menu.showMenu(INGAME);
			awesomeMusic.SetVolume(MUSICVOLUME);
		}


		//Inputs
		if(player1Alive)
			player1 -> input(delta);
		if(numberOfPlayers == 2 && player2Alive)
			player2 -> input(delta);

		
		//AI
		intel.go(delta);

		////Collision detection between friendlies
		//if(numberOfPlayers == 2 && player2Alive && player1Alive)
		//{
		//	hurty = player1->collisionCheck(player2->getCraft());
		//	if(hurty != 0)
		//	{
		//		player2Alive = player2->hurt(hurty);
		//		if(!player2Alive)
		//			player2->die();
		//	}
		//	
		//	hurty = player2->collisionCheck(player1->getCraft());
		//	if(hurty != 0)
		//	{
		//		player1Alive = player1->hurt(hurty);
		//		if(!player1Alive)
		//			player1->die();
		//	}
		//}


		//Collision detection to AI
		int hurty;

		for(int i = 0; i < intel.getNumberEnemies(); i++)
		{
			if(intel.getCraft(i) != NULL)
			{
		
				hurty = player1->collisionCheck(intel.getCraft(i));
				if(hurty != 0)
				{
					alive = intel.hurt(i, hurty);
					if(!alive)
					{
						player1->score(intel.getScore());
						intel.kill(i);
					}
				}
			}

			if(numberOfPlayers == 2 && intel.getCraft(i) != NULL)
			{
				hurty = player2->collisionCheck(intel.getCraft(i));
				if(hurty != 0)
				{
					alive = intel.hurt(i, hurty);
					if(!alive)
					{
						player2->score(intel.getScore());
						intel.kill(i);
					}
				}
			}
		}

		
		//Collision detection from AI
		for(int i = 0; i < intel.getNumberEnemies(); i++)
		{
			if(player1Alive)
			{
				hurty = intel.collisionCheck(i, player1->getCraft());
				if(hurty != 0)
				{
					player1Alive = player1->hurt(hurty);
					if(!player1Alive)
						player1->die();
				}
			}

			if(numberOfPlayers == 2 && player2Alive)
			{
				hurty = intel.collisionCheck(i, player2->getCraft());
				if(hurty != 0)
				{
					player2Alive = player2->hurt(hurty);
					if(!player2Alive)
						player2->die();
				}
			}
		}
	

		//Drawing things (lowest first)
		for(int i = 0; i < STARS; i++)
			stars[i]->drawStar(delta);

		if(numberOfPlayers == 2)
		{
			if(!player2Alive)
				player2->draw();
		}
		if(!player1Alive)
			player1->draw();

		intel.drawLayzars(delta);
		if(numberOfPlayers == 2)
			player2->drawLayzars(delta);
		player1->drawLayzars(delta);

		intel.drawEnemies();
		if(numberOfPlayers == 2)
		{
			if(player2Alive)
				player2->draw();
		}
		if(player1Alive)
			player1->draw();

		if(numberOfPlayers == 2)
		{
			player2->drawHealth();
			player2->drawScore();
		}
		player1->drawHealth();
		player1->drawScore();

		xwing.Display();
	}//End game loop

	std::cout << "Terminated.\n";

	return EXIT_SUCCESS;
}